<?php
error_reporting(E_ALL ^ E_DEPRECATED); 
define("COLOR_LEV1", "#7F7F7F");
define("COLOR_LEV2", "#929292");
//define("COLOR_LEV2", "red");
define("COLOR_LEV3", "#A4A3A3");
define("COLOR_LEV4", "#B7B6B6");
define("COLOR_LEV5", "#CBCACA");
define("COLOR_LEV6", "#DEDCDC");
define("COLOR_LEV7", "#EFEEEE");

define('AJAX_SCRIPT', true);
require_once('../../config.php');
require_once('./lib.php');

$tim = required_param('tim', PARAM_TEXT);
$survey_id = required_param('survey_id', PARAM_TEXT);

if($tim=='surveyBenchmark')
{   
    $mylib = new benchmark_management();
    $dblib = new survey_management();
    $deepest = $mylib->getDepthOfSurvey($survey_id);
    echo "<input type='hidden' name='cur_survey_id' id='cur_survey_id' value='".$survey_id."'/>";
    
    //COL FILTER
    echo "Column Filter";
    echo "<table><tr><td>";
    echo "I&#09;";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='colIoperator' id='colIoperator'><option value=\"0\">Select operator</option><option value=\"<\"><</option><option value=\">\">></option><option value=\"=\">=</option><option value=\"<=\"><=</option><option value=\">=\">>=</option></select>&nbsp;";
    echo "<input type='text' name='colIqty' id='colIqty' style=\"width:30px;\"/><br/>";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='colConnection1' id='colConnection1'><option value='AND'>AND</option><option value='OR'>OR</option></select>";
    echo "</td>";
    
    echo "<td>";
    echo "T&#09;";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='colToperator' id='colToperator'><option value=\"0\">Select operator</option><option value=\"<\"><</option><option value=\">\">></option><option value=\"=\">=</option><option value=\"<=\"><=</option><option value=\">=\">>=</option></select>&nbsp;";
    echo "<input type='text' name='colTqty' id='colTqty' style=\"width:30px;\"/><br/>";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='colConnection2' id='colConnection2'><option value='AND'>AND</option><option value='OR'>OR</option></select>";
    echo "</td>";
    
    echo "<td>";
    echo "U&#09;";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='colUoperator' id='colUoperator'><option value=\"0\">Select operator</option><option value=\"<\"><</option><option value=\">\">></option><option value=\"=\">=</option><option value=\"<=\"><=</option><option value=\">=\">>=</option></select>&nbsp;";
    echo "<input type='text' name='colUqty' id='colUqty' style=\"width:30px;\"/><br/>";
    echo "</td>";
    
    echo "<td>";
    echo "</td></tr></table>";
    //---------------------------------------------------------------
    //ROW FILTER
    echo "Row Filter";
    echo "<table><tr><td>";
    echo "I&#09;";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='rowIoperator' id='rowIoperator'><option value=\"0\">Select operator</option><option value=\"<\"><</option><option value=\">\">></option><option value=\"=\">=</option><option value=\"<=\"><=</option><option value=\">=\">>=</option></select>&nbsp;";
    echo "<input type='text' name='rowIqty' id='rowIqty' style=\"width:30px;\"/><br/>";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='rowConnection1' id='rowConnection1'><option value='AND'>AND</option><option value='OR'>OR</option></select>";
    echo "</td>";
    
    echo "<td>";
    echo "T&#09;";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='rowToperator' id='rowToperator'><option value=\"0\">Select operator</option><option value=\"<\"><</option><option value=\">\">></option><option value=\"=\">=</option><option value=\"<=\"><=</option><option value=\">=\">>=</option></select>&nbsp;";
    echo "<input type='text' name='rowTqty' id='rowTqty' style=\"width:30px;\"/><br/>";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='rowConnection2' id='rowConnection2'><option value='AND'>AND</option><option value='OR'>OR</option></select>";
    echo "</td>";
    
    echo "<td>";
    echo "U&#09;";
    echo "</td>";
    
    echo "<td>";
    echo "<select name='rowUoperator' id='rowUoperator'><option value=\"0\">Select operator</option><option value=\"<\"><</option><option value=\">\">></option><option value=\"=\">=</option><option value=\"<=\"><=</option><option value=\">=\">>=</option></select>&nbsp;";
    echo "<input type='text' name='rowUqty' id='rowUqty' style=\"width:30px;\"/><br/>";
    echo "</td>";
    
    echo "<td>";
    echo "</td></tr></table><br/>";
    
    echo "Column Color:<select name='colorCol' id='colorCol'><option style='background-color:#D16587;' value='#D16587'>Red</option><option style='background-color:#00FF00;' value='#00FF00'>Green</option><option style='background-color:#FFFF00;' value='#FFFF00'>Yellow</option><option style='background-color:#00FFFF;' value='#00FFFF'>Blue</option></select>";
    echo "<br/>Row Color:<select name='colorRow' id='colorRow'><option style='background-color:#D16587;' value='#D16587'>Red</option><option style='background-color:#00FF00;' value='#00FF00'>Green</option><option style='background-color:#FFFF00;' value='#FFFF00'>Yellow</option><option style='background-color:#00FFFF;' value='#00FFFF'>Blue</option></select>";
    echo "<br/>";
    
    echo "<br/>Filter Description:<input name='f_description' id='f_description' type='text' value='No Description' length='200' size='200px'/><br/>";
    
    echo "<img name=\"loader\" id=\"loader\" src=\"../css/images/ajax-loader.gif\" style=\"display:none;\">";
    
    echo "<br/><input type='button' name='btnApplyFilter' id='btnApplyFilter' value='Apply' onclick='parent.filteringMatrix();'/>";
    //echo "<input type='button' name='btnClearFilter' id='btnClearFilter' value='Clear' onclick='parent.clearFilteringMatrix();'/>";
    
    echo "<div  id=\"historyFilter\" name=\"historyFilter\">History Filter:<br/>";    
    //Reload old filters
    $filters = $mylib->reloadFilterOfSurvey($survey_id);
    //echo "reloaded filter:".count($filters)."<br/>";
        foreach($filters as $filter){
            $filterdetail = array(
                    "id"=>$filter->id,
                    "coliop"=>$filter->coliop,
                    "colqtyi"=>$filter->colqtyi,
                    "colconnection1"=>$filter->colconnection1,
                    "coltop"=>$filter->coltop,
                    "colqtyt"=>$filter->colqtyt,
                    "colconnection2"=>$filter->colconnection2,
                    "coluop"=>$filter->coluop,
                    "colqtyu"=>$filter->colqtyu,
                    "rowiop"=>$filter->rowiop,
                    "rowqtyi"=>$filter->rowqtyi,
                    "rowconnection1"=>$filter->rowconnection1,
                    "rowtop"=>$filter->rowtop,
                    "rowqtyt"=>$filter->rowqtyt,
                    "rowconnection2"=>$filter->rowconnection2,
                    "rowuop"=>$filter->rowuop,
                    "rowqtyu"=>$filter->rowqtyu,
                    "f_survey"=>$filter->f_survey,
                    "f_description"=>$filter->f_description,
                    "f_color_col"=>$filter->f_color_col,
                    "f_color_row"=>$filter->f_color_row
            );
            
            //echo $filter->id.$filter->coliop.$filter->colqtyi.$filter->colqtyi.$filter->colconnection1.$filter->coltop;
            //echo $filter->colqtyt.$filter->colconnection2.$filter->coluop.$filter->colqtyu;
            //echo "dungarray:".$filterdetail["coliop"];
            
            $colFilter = "";
            if(strcmp($filterdetail["coliop"],"0")){
                $colFilter .= "I ".$filterdetail["coliop"]." ".$filterdetail["colqtyi"];    
                if(strcmp($filterdetail["coltop"],"0") != "0"){
                    $colFilter .= $filterdetail["colconnection1"] ." T ".$filterdetail["coltop"]." ".$filterdetail["colqtyt"];
                    if (strcmp($filterdetail["coluop"],"0"))
                        $colFilter .= $filterdetail["colconnection2"] ." U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];
                }else if(strcmp($filterdetail["coluop"],"0")){
                    $colFilter .= $filterdetail["colconnection2"] ." U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];
                }
            }
            else if(strcmp($filterdetail["coltop"],"0")){
                $colFilter .= " T ".$filterdetail["coltop"]." ".$filterdetail["colqtyt"];
                if(strcmp($filterdetail["coluop"],"0")){
                    $colFilter .= $filterdetail["colconnection2"] ." U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];    
                }
            }else if(strcmp($filterdetail["coluop"],"0")){
                $colFilter .= " U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];
            }
            
            $rowFilter = "";
            if(strcmp($filterdetail["rowiop"],"0")){
                $rowFilter .= "I ".$filterdetail["rowiop"]." ".$filterdetail["rowqtyi"];    
                if(strcmp($filterdetail["rowtop"],"0")){
                    $rowFilter .= $filterdetail["rowconnection1"] ." T ".$filterdetail["rowtop"]." ".$filterdetail["rowqtyt"];
                    if (strcmp($filterdetail["rowuop"],"0"))
                        $rowFilter .= $filterdetail["rowconnection2"] ." U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];
                }else if(strcmp($filterdetail["rowuop"],"0")){
                    $rowFilter .= $filterdetail["rowconnection2"] ." U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];
                }
            }
            else if(strcmp($filterdetail["rowtop"], "0")){
                $rowFilter .= " T ".$filterdetail["rowtop"]." ".$filterdetail["rowqtyt"];
                if(strcmp($filterdetail["rowuop"], "0")){
                    $rowFilter .= $filterdetail["rowconnection2"] ." U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];    
                }
            }else if(strcmp($filterdetail["rowuop"], "0")){
                $rowFilter .= " U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];
            }
            

            $paramForToggleFilterMethod = 
                    $filterdetail["coliop"]."/".
                    $filterdetail["colqtyi"]."/".
                    $filterdetail["colconnection1"]."/".
                    $filterdetail["coltop"]."/".
                    $filterdetail["colqtyt"]."/".
                    $filterdetail["colconnection2"]."/".
                    $filterdetail["coluop"]."/".
                    $filterdetail["colqtyu"]."/".
                    $filterdetail["rowiop"]."/".
                    $filterdetail["rowqtyi"]."/".
                    $filterdetail["rowconnection1"]."/".
                    $filterdetail["rowtop"]."/".
                    $filterdetail["rowqtyt"]."/".
                    $filterdetail["rowconnection2"]."/".
                    $filterdetail["rowuop"]."/".
                    $filterdetail["rowqtyu"]."/".
                    $filterdetail["f_color_col"]."/".
                    $filterdetail["f_color_row"];
                    
            echo $filterdetail["f_description"];
            echo "<input type=\"checkbox\" name=\"cbx_filter_enabler\" id=\"cbx_filter_enabler\" onchange=\"parent.refreshFilters();\" value=\"".$paramForToggleFilterMethod."\"/>";
            echo "<a href=\"#\" onclick=\"parent.deleteFilter(".$filterdetail["id"].");return false;\">X</a>";
            echo "<div style=\"width:200px;background-color:".$filterdetail["f_color_col"].";\">".$colFilter."</div>";
            echo "<div style=\"width:200px;background-color:".$filterdetail["f_color_row"].";\">".$rowFilter."</div>";            
            echo "-----------------END-----------------<br/>";
        }
    echo "</div>";
    
    echo "<table class=\"benchmark\" border=\"1\" id=\"matrixTable\">";
    //Display LOs (hierachy and merged cells)
    $LoStrArray = array();
    $DeepestLOsCodeArray = array();
    $DeepestLOsIdArray = array();
    for($i = 1; $i <= $deepest; $i++){
        if($i == 1){
            $LoStrArray[$i] = "<tr border=\"1\"><td rowspan='".$deepest."'></td>";
        }else{
            $LoStrArray[$i] = "<tr>";    
        }
    }
        displayChildLoDownToLev($survey_id, 1, $deepest, "0", $dblib, $LoStrArray, 0, $DeepestLOsCodeArray, $DeepestLOsIdArray);
    for($i=1; $i<=$deepest; $i++){
        $LoStrArray[$i] .= "</tr>";
        echo $LoStrArray[$i];
    }
    
    $hiddenLOsStr = "";
    $hiddenSubjectsStr = "";
    
    foreach($DeepestLOsCodeArray as $value){
        $hiddenLOsStr .= $value.",";    
    }
    
    
    //Display Subjects    
        //Get Majors of Program
        $majors = $DB->get_records("surveyed_majors", array("sm_survey"=>$survey_id));
        
        $totalOfDeepestLOs = count($DeepestLOsCodeArray);
        $temporaryTInstructorList = array();
        $instructor = null;
        $instructorNumber = 0;
        $runner = 1;
        foreach($majors as $major){
            
            $sql = "select * ";
            $sql .= "from mdl_surveyed_subjects ss ";
            $sql .= "where ss_survey = $survey_id ";
            $sql .= "and ss_subject in ";
            	$sql .= "( ";
            		$sql .= "select sb.subject_id  ";
            		$sql .= "from mdl_subject_block sb ";
            		$sql .= "where sb.blockofknowledge_id in  ";
            			$sql .= "( ";
            			$sql .= "select bok.id ";
            			$sql .= "from mdl_block_of_knowledge bok ";
            			$sql .= "where id in (select mb.blockofknowledge_id from mdl_major_block mb where major_id = ".$major->sm_major.") ";
            			$sql .= ") ";
            	$sql .= "); ";
            
            //$subjects = $DB->get_records("surveyed_subjects", array("ss_survey"=>$survey_id));
            $subjects = $DB->get_records_sql($sql,array());
            //A ""Group By" Major
            echo "<tr style=\"cursor:pointer;\" onclick=\"parent.toggleMajorBlock(".$major->sm_major.");\"><td style=\"width:300px;\">".$major->sm_name."</td>";
            for($i = 0; $i < $totalOfDeepestLOs; $i++){
                echo "<td>&nbsp;</td>";
            }
            echo "</tr>";
            //Subjects in major
            foreach($subjects as $subject){
                echo "<tr name=\"major-".$major->sm_major."\" id=\"major-".$major->sm_major."\" ><td id=\"sub".$subject->ss_subject."\" name=\"sub".$subject->ss_subject."\" style=\"width:300px;\">".$subject->ss_code."</td>";
                    for($i = 0; $i < $totalOfDeepestLOs; $i++){
                        $qtyI = $mylib->countITUValueOf($survey_id,$subject->ss_subject, $DeepestLOsCodeArray[$i], 1);
                        $qtyT = $mylib->countITUValueOf($survey_id,$subject->ss_subject, $DeepestLOsCodeArray[$i], 2);
                        $qtyU = $mylib->countITUValueOf($survey_id,$subject->ss_subject, $DeepestLOsCodeArray[$i], 3);
                        
                        echo "<td id=\"sub".$subject->ss_subject."-lo".$DeepestLOsCodeArray[$i]."\"";
                        echo " name=\"sub".$subject->ss_subject."-lo".$DeepestLOsCodeArray[$i]."\">&nbsp;";
                        /**********************/
                        if($qtyI != "0"){
                            $temporaryTInstructorList = $mylib->getOwnerOfITUValue($survey_id, $subject->ss_subject, $DeepestLOsCodeArray[$i], 1);
                            echo "<div style=\"cursor:pointer;\" onclick=\"parent.displayInstructorOfITUValue(this);\" title=\"";
                            $instructorNumber = count($temporaryTInstructorList);
                            $runner = 1;
                                foreach($temporaryTInstructorList as $instructor){
                                    if($runner == $instructorNumber){
                                        echo "<a href='reviewSurvey.php' onclick='parent.reviewSurvey(".$subject->ss_subject.",".$survey_id.",".$instructor->user_id.",".$DeepestLOsIdArray[$i].");return false;'>".$instructor->instructor."</a>";    
                                    }else{
                                        echo "<a href='reviewSurvey.php' onclick='parent.reviewSurvey(".$subject->ss_subject.",".$survey_id.",".$instructor->user_id.",".$DeepestLOsIdArray[$i].");return false;'>".$instructor->instructor."</a>,";
                                    }
                                    $runner++;
                                }
                            echo "\">I(".$qtyI.")</div>";
                        }
                        
                        if($qtyT != "0"){
                            $temporaryTInstructorList = $mylib->getOwnerOfITUValue($survey_id, $subject->ss_subject, $DeepestLOsCodeArray[$i], 2);
                            echo "<div style=\"cursor:pointer;\" onclick=\"parent.displayInstructorOfITUValue(this);\" title=\"";
                            $instructorNumber = count($temporaryTInstructorList);
                            $runner = 1;
                                foreach($temporaryTInstructorList as $instructor){
                                    if($runner == $instructorNumber){
                                        echo "<a href='reviewSurvey.php' onclick='parent.reviewSurvey(".$subject->ss_subject.",".$survey_id.",".$instructor->user_id.",".$DeepestLOsIdArray[$i].");return false;'>".$instructor->instructor."</a>";    
                                    }else{
                                        echo "<a href='reviewSurvey.php' onclick='parent.reviewSurvey(".$subject->ss_subject.",".$survey_id.",".$instructor->user_id.",".$DeepestLOsIdArray[$i].");return false;'>".$instructor->instructor."</a>,";
                                    }
                                    $runner++;
                                }
                            echo "\">T(".$qtyT.")</div>";    
                        }
                        
                        if($qtyU != "0"){
                            $temporaryTInstructorList = $mylib->getOwnerOfITUValue($survey_id, $subject->ss_subject, $DeepestLOsCodeArray[$i], 3);
                            echo "<div style=\"cursor:pointer;\" onclick=\"parent.displayInstructorOfITUValue(this);\" title=\"";
                            $instructorNumber = count($temporaryTInstructorList);
                            $runner = 1;
                                foreach($temporaryTInstructorList as $instructor){
                                    if($runner == $instructorNumber){
                                        echo "<a href='reviewSurvey.php' onclick='parent.reviewSurvey(".$subject->ss_subject.",".$survey_id.",".$instructor->user_id.",".$DeepestLOsIdArray[$i].");return false;'>".$instructor->instructor."</a>";    
                                    }else{
                                        echo "<a href='reviewSurvey.php' onclick='parent.reviewSurvey(".$subject->ss_subject.",".$survey_id.",".$instructor->user_id.",".$DeepestLOsIdArray[$i].");return false;'>".$instructor->instructor."</a>,";
                                    }
                                    $runner++;
                                }  
                            echo "\">U(".$qtyU.")</div>";  
                        }
                        /**********************/
                        echo "&nbsp;</td>";
                    }
                    $hiddenSubjectsStr .= $subject->ss_subject.",";
                echo "</tr>";                
            }
        }            
    echo "</table>";
    
    echo "<input type='hidden' name='losStr' id='losStr' value='".$hiddenLOsStr."'/>";
    echo "<input type='hidden' name='subjectsStr' id='subjectsStr' value='".$hiddenSubjectsStr."'/>";
    
}else if($tim=='warn'){
    $mylib = new benchmark_management();
    
    $colIOp =  $_POST["colIOp"];
    $colQtyI = $_POST["colQtyI"];
    $colConnection1= $_POST["colConnection1"];
    $colTOp= $_POST["colTOp"];
    $colQtyT= $_POST["colQtyT"];
    $colConnection2= $_POST["colConnection2"];
    $colUOp = $_POST["colUOp"];
    $colQtyU = $_POST["colQtyU"];
    $rowIOp = $_POST["rowIOp"];
    $rowQtyI= $_POST["rowQtyI"];
    $rowConnection1= $_POST["rowConnection1"];
    $rowTOp= $_POST["rowTOp"];
    $rowQtyT= $_POST["rowQtyT"];
    $rowConnection2= $_POST["rowConnection2"];
    $rowUOp= $_POST["rowUOp"];
    $rowQtyU= $_POST["rowQtyU"];    
    
    $fColorCol = $_POST["fColorCol"];
    $fColorRow = $_POST["fColorRow"];
    
    $filterRowSql = "";
    $filterColSql = "";
    
    $f_description =  $_POST["f_description"];
    
/*-------------------------Filter ROW (SUBJECTS) BEGIN---------------------------------*/
$filterRowSql .= "select distinct ss.ss_subject from mdl_surveyed_subjects ss where ss_survey = ".$survey_id; 
$arrayOfFragmentSqlRow = array();
//I condition
if($rowIOp != "0"){
    $arrayOfFragmentSqlRow[1] = "(select count(*) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id." and sts_subject_id = ss.ss_subject) and slov_value = 1) ".$rowIOp." ".$rowQtyI;    
}
//connector 1
if($rowConnection1 != "0"){
    $arrayOfFragmentSqlRow[2] = " ".$rowConnection1." ";
}
//T condition
if($rowTOp != "0"){
    $arrayOfFragmentSqlRow[3] = "(select count(*) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id." and sts_subject_id = ss.ss_subject) and slov_value = 2) ".$rowTOp." ".$rowQtyT;
}
//connector 2
if($rowConnection2 != "0"){
    $arrayOfFragmentSqlRow[4] = " ".$rowConnection2." ";
}
//U condition
if($rowUOp != "0"){
    $arrayOfFragmentSqlRow[5] = "(select count(*) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id." and sts_subject_id = ss.ss_subject) and slov_value = 3 ) ".$rowUOp." ".$rowQtyU;
}
//Combine all elements
if(!is_null($arrayOfFragmentSqlRow[1]) || !is_null($arrayOfFragmentSqlRow[3]) || !is_null($arrayOfFragmentSqlRow[5])){
    $filterRowSql .= " AND ";
}
if(!is_null($arrayOfFragmentSqlRow[1])){
    $filterRowSql .= $arrayOfFragmentSqlRow[1];
        if(!is_null($arrayOfFragmentSqlRow[3])){
            $filterRowSql .= $arrayOfFragmentSqlRow[2].$arrayOfFragmentSqlRow[3];
            if(!is_null($arrayOfFragmentSqlRow[5])){
                $filterRowSql .= $arrayOfFragmentSqlRow[4].$arrayOfFragmentSqlRow[5];    
            }
        }else if(!is_null($arrayOfFragmentSqlRow[5])){
            $filterRowSql .= $arrayOfFragmentSqlRow[4].$arrayOfFragmentSqlRow[5];
        }
}else if(!is_null($arrayOfFragmentSqlRow[3])){
    $filterRowSql .= $arrayOfFragmentSqlRow[3];
    if(!is_null($arrayOfFragmentSqlRow[5])){
        $filterRowSql .= $arrayOfFragmentSqlRow[4].$arrayOfFragmentSqlRow[5];
    }
}else if(!is_null($arrayOfFragmentSqlRow[5])){
    $filterRowSql .= $arrayOfFragmentSqlRow[5];
}

//Only filter if any rule created
if(!is_null($arrayOfFragmentSqlRow[1]) || !is_null($arrayOfFragmentSqlRow[3]) || !is_null($arrayOfFragmentSqlRow[5])){
    $highlightedLOs = $mylib->filterRowSubjects($filterRowSql);
}
//echo $filterRowSql;
$highlightedLOStr = "";
$i = 1;
$endOfI = count($highlightedLOs);
foreach($highlightedLOs as $oneHighlightedLos){
    if($i == $endOfI){
        $highlightedLOStr .= $oneHighlightedLos->ss_subject;
    }else{
        $highlightedLOStr .= $oneHighlightedLos->ss_subject.",";
    }
    $i++;
}
echo $highlightedLOStr;
/*-------------------------Filter ROW (SUBJECTS) END---------------------------------*/
echo "/";
/*-------------------------Filter COL (LOS) BEGIN--------------------------------*/
$filterColSql = "select distinct sl_lo from mdl_surveyed_los sl where sl_survey =" .$survey_id;
$arrayOfFragmentSqlCol = array();
//I condition
if($colIOp != "0"){
    $arrayOfFragmentSqlCol[1] = "(select count(id) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id.") and slov_lo = sl.sl_lo and slov_value = 1) ".$colIOp." ".$colQtyI;    
}
//connector 1
if($colConnection1 != "0"){
    $arrayOfFragmentSqlCol[2] = " ".$colConnection1." ";
}
//T condition
if($colTOp != "0"){
    $arrayOfFragmentSqlCol[3] = "(select count(id) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id.") and slov_lo = sl.sl_lo and slov_value = 2) ".$colTOp." ".$colQtyT;    
}
//connector 2
if($colConnection2 != "0"){
    $arrayOfFragmentSqlCol[4] = " ".$colConnection2." ";
}
//U condition
if($colUOp != "0"){
    $arrayOfFragmentSqlCol[5] = "(select count(id) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id.") and slov_lo = sl.sl_lo and slov_value = 3) ".$colUOp." ".$colQtyU;    
}
//Combine all elements
if(!is_null($arrayOfFragmentSqlCol[1]) || !is_null($arrayOfFragmentSqlCol[3]) || !is_null($arrayOfFragmentSqlCol[5])){
    $filterColSql .= " AND ";
}

if(!is_null($arrayOfFragmentSqlCol[1])){
    $filterColSql .= $arrayOfFragmentSqlCol[1];
        if(!is_null($arrayOfFragmentSqlCol[3])){
            $filterColSql .= $arrayOfFragmentSqlCol[2].$arrayOfFragmentSqlCol[3];
            if(!is_null($arrayOfFragmentSqlCol[5])){
                $filterColSql .= $arrayOfFragmentSqlCol[4].$arrayOfFragmentSqlCol[5];    
            }
        }else if(!is_null($arrayOfFragmentSqlCol[5])){
            $filterColSql .= $arrayOfFragmentSqlCol[4].$arrayOfFragmentSqlCol[5];
        }
}else if(!is_null($arrayOfFragmentSqlCol[3])){
    $filterColSql .= $arrayOfFragmentSqlCol[3];
    if(!is_null($arrayOfFragmentSqlCol[5])){
        $filterColSql .= $arrayOfFragmentSqlCol[4].$arrayOfFragmentSqlCol[5];
    }
}else if(!is_null($arrayOfFragmentSqlCol[5])){
    $filterColSql .= $arrayOfFragmentSqlCol[5];
}
//Only filter if any rule created
if(!is_null($arrayOfFragmentSqlCol[1]) || !is_null($arrayOfFragmentSqlCol[3]) || !is_null($arrayOfFragmentSqlCol[5])){
    $highlightedSubjects = $mylib->filterColLOs($filterColSql);
}
//echo $filterColSql;
$highlightedSubjectsStr = "";
$ii = 1;
$endOfII = count($highlightedSubjects);
foreach($highlightedSubjects as $oneHighlightedSubject){
    if($ii == $endOfII){
        $highlightedSubjectsStr .= $oneHighlightedSubject->sl_lo;
    }else{
        $highlightedSubjectsStr .= $oneHighlightedSubject->sl_lo.",";
    }
    $ii++;
}
echo $highlightedSubjectsStr;

//echo $fColorCol."-".$fColorRow;
/*-------------------------Filter COL (LOS) END--------------------------------*/

//Store filter
    if(!is_null($arrayOfFragmentSqlRow[1]) || !is_null($arrayOfFragmentSqlRow[3]) || !is_null($arrayOfFragmentSqlRow[5]) || !is_null($arrayOfFragmentSqlCol[1]) || !is_null($arrayOfFragmentSqlCol[3]) || !is_null($arrayOfFragmentSqlCol[5])){
        $newFilterID = 0;
        $newFilterID = $mylib->saveFilterBenchmark(htmlspecialchars($colIOp),$colQtyI,$colConnection1,htmlspecialchars($colTOp),$colQtyT,$colConnection2,htmlspecialchars($colUOp),$colQtyU,htmlspecialchars($rowIOp),$rowQtyI,$rowConnection1,htmlspecialchars($rowTOp),$rowQtyT,$rowConnection2,htmlspecialchars($rowUOp),$rowQtyU, htmlspecialchars($f_description), $survey_id, $fColorCol, $fColorRow);
        echo "/".$newFilterID;
    }
}else if($tim=='resfreshWarn'){
    $mylib = new benchmark_management();
    
    $colIOp =  $_POST["colIOp"];
    $colQtyI = $_POST["colQtyI"];
    $colConnection1= $_POST["colConnection1"];
    $colTOp= $_POST["colTOp"];
    $colQtyT= $_POST["colQtyT"];
    $colConnection2= $_POST["colConnection2"];
    $colUOp = $_POST["colUOp"];
    $colQtyU = $_POST["colQtyU"];
    $rowIOp = $_POST["rowIOp"];
    $rowQtyI= $_POST["rowQtyI"];
    $rowConnection1= $_POST["rowConnection1"];
    $rowTOp= $_POST["rowTOp"];
    $rowQtyT= $_POST["rowQtyT"];
    $rowConnection2= $_POST["rowConnection2"];
    $rowUOp= $_POST["rowUOp"];
    $rowQtyU= $_POST["rowQtyU"];    
    
    $fColorCol = $_POST["fColorCol"];
    $fColorRow = $_POST["fColorRow"];
    
    $filterRowSql = "";
    $filterColSql = "";
    
/*-------------------------Filter ROW (SUBJECTS) BEGIN---------------------------------*/
$filterRowSql .= "select distinct ss.ss_subject from mdl_surveyed_subjects ss where ss_survey = ".$survey_id; 
$arrayOfFragmentSqlRow = array();
//I condition
if($rowIOp != "0"){
    $arrayOfFragmentSqlRow[1] = "(select count(*) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id." and sts_subject_id = ss.ss_subject) and slov_value = 1) ".$rowIOp." ".$rowQtyI;    
}
//connector 1
if($rowConnection1 != "0"){
    $arrayOfFragmentSqlRow[2] = " ".$rowConnection1." ";
}
//T condition
if($rowTOp != "0"){
    $arrayOfFragmentSqlRow[3] = "(select count(*) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id." and sts_subject_id = ss.ss_subject) and slov_value = 2) ".$rowTOp." ".$rowQtyT;
}
//connector 2
if($rowConnection2 != "0"){
    $arrayOfFragmentSqlRow[4] = " ".$rowConnection2." ";
}
//U condition
if($rowUOp != "0"){
    $arrayOfFragmentSqlRow[5] = "(select count(*) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id." and sts_subject_id = ss.ss_subject) and slov_value = 3 ) ".$rowUOp." ".$rowQtyU;
}
//Combine all elements
if(!is_null($arrayOfFragmentSqlRow[1]) || !is_null($arrayOfFragmentSqlRow[3]) || !is_null($arrayOfFragmentSqlRow[5])){
    $filterRowSql .= " AND ";
}
if(!is_null($arrayOfFragmentSqlRow[1])){
    $filterRowSql .= $arrayOfFragmentSqlRow[1];
        if(!is_null($arrayOfFragmentSqlRow[3])){
            $filterRowSql .= $arrayOfFragmentSqlRow[2].$arrayOfFragmentSqlRow[3];
            if(!is_null($arrayOfFragmentSqlRow[5])){
                $filterRowSql .= $arrayOfFragmentSqlRow[4].$arrayOfFragmentSqlRow[5];    
            }
        }else if(!is_null($arrayOfFragmentSqlRow[5])){
            $filterRowSql .= $arrayOfFragmentSqlRow[4].$arrayOfFragmentSqlRow[5];
        }
}else if(!is_null($arrayOfFragmentSqlRow[3])){
    $filterRowSql .= $arrayOfFragmentSqlRow[3];
    if(!is_null($arrayOfFragmentSqlRow[5])){
        $filterRowSql .= $arrayOfFragmentSqlRow[4].$arrayOfFragmentSqlRow[5];
    }
}else if(!is_null($arrayOfFragmentSqlRow[5])){
    $filterRowSql .= $arrayOfFragmentSqlRow[5];
}

//Only filter if any rule created
if(!is_null($arrayOfFragmentSqlRow[1]) || !is_null($arrayOfFragmentSqlRow[3]) || !is_null($arrayOfFragmentSqlRow[5])){
    $highlightedLOs = $mylib->filterRowSubjects($filterRowSql);
}
//echo $filterRowSql;
$highlightedLOStr = "";
$i = 1;
$endOfI = count($highlightedLOs);
foreach($highlightedLOs as $oneHighlightedLos){
    if($i == $endOfI){
        $highlightedLOStr .= $oneHighlightedLos->ss_subject;
    }else{
        $highlightedLOStr .= $oneHighlightedLos->ss_subject.",";
    }
    $i++;
}
echo $highlightedLOStr;
/*-------------------------Filter ROW (SUBJECTS) END---------------------------------*/
echo "/";
/*-------------------------Filter COL (LOS) BEGIN--------------------------------*/
$filterColSql = "select distinct sl_lo from mdl_surveyed_los sl where sl_survey =" .$survey_id;
$arrayOfFragmentSqlCol = array();
//I condition
if($colIOp != "0"){
    $arrayOfFragmentSqlCol[1] = "(select count(id) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id.") and slov_lo = sl.sl_lo and slov_value = 1) ".$colIOp." ".$colQtyI;    
}
//connector 1
if($colConnection1 != "0"){
    $arrayOfFragmentSqlCol[2] = " ".$colConnection1." ";
}
//T condition
if($colTOp != "0"){
    $arrayOfFragmentSqlCol[3] = "(select count(id) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id.") and slov_lo = sl.sl_lo and slov_value = 2) ".$colTOp." ".$colQtyT;    
}
//connector 2
if($colConnection2 != "0"){
    $arrayOfFragmentSqlCol[4] = " ".$colConnection2." ";
}
//U condition
if($colUOp != "0"){
    $arrayOfFragmentSqlCol[5] = "(select count(id) as qty from mdl_slo_value where slov_sts in (select id from mdl_itus_teachers_subjects where sts_survey_id = ".$survey_id.") and slov_lo = sl.sl_lo and slov_value = 3) ".$colUOp." ".$colQtyU;    
}
//Combine all elements
if(!is_null($arrayOfFragmentSqlCol[1]) || !is_null($arrayOfFragmentSqlCol[3]) || !is_null($arrayOfFragmentSqlCol[5])){
    $filterColSql .= " AND ";
}

if(!is_null($arrayOfFragmentSqlCol[1])){
    $filterColSql .= $arrayOfFragmentSqlCol[1];
        if(!is_null($arrayOfFragmentSqlCol[3])){
            $filterColSql .= $arrayOfFragmentSqlCol[2].$arrayOfFragmentSqlCol[3];
            if(!is_null($arrayOfFragmentSqlCol[5])){
                $filterColSql .= $arrayOfFragmentSqlCol[4].$arrayOfFragmentSqlCol[5];    
            }
        }else if(!is_null($arrayOfFragmentSqlCol[5])){
            $filterColSql .= $arrayOfFragmentSqlCol[4].$arrayOfFragmentSqlCol[5];
        }
}else if(!is_null($arrayOfFragmentSqlCol[3])){
    $filterColSql .= $arrayOfFragmentSqlCol[3];
    if(!is_null($arrayOfFragmentSqlCol[5])){
        $filterColSql .= $arrayOfFragmentSqlCol[4].$arrayOfFragmentSqlCol[5];
    }
}else if(!is_null($arrayOfFragmentSqlCol[5])){
    $filterColSql .= $arrayOfFragmentSqlCol[5];
}
//Only filter if any rule created
if(!is_null($arrayOfFragmentSqlCol[1]) || !is_null($arrayOfFragmentSqlCol[3]) || !is_null($arrayOfFragmentSqlCol[5])){
    $highlightedSubjects = $mylib->filterColLOs($filterColSql);
}
//echo $filterColSql;
$highlightedSubjectsStr = "";
$ii = 1;
$endOfII = count($highlightedSubjects);
foreach($highlightedSubjects as $oneHighlightedSubject){
    if($ii == $endOfII){
        $highlightedSubjectsStr .= $oneHighlightedSubject->sl_lo;
    }else{
        $highlightedSubjectsStr .= $oneHighlightedSubject->sl_lo.",";
    }
    $ii++;
}
echo $highlightedSubjectsStr;
/*-------------------------Filter COL (LOS) END--------------------------------*/
}else if($tim=='deleteFilter'){
        //Delete Filter
        $idFilter = $_GET["filter"];
        $mylib = new benchmark_management();
        $mylib->deleteFilter($idFilter);
        
        //Reload filters list
        $filters = $mylib->reloadFilterOfSurvey($survey_id);
        foreach($filters as $filter){
            $filterdetail = array(
                    "id"=>$filter->id,
                    "coliop"=>$filter->coliop,
                    "colqtyi"=>$filter->colqtyi,
                    "colconnection1"=>$filter->colconnection1,
                    "coltop"=>$filter->coltop,
                    "colqtyt"=>$filter->colqtyt,
                    "colconnection2"=>$filter->colconnection2,
                    "coluop"=>$filter->coluop,
                    "colqtyu"=>$filter->colqtyu,
                    "rowiop"=>$filter->rowiop,
                    "rowqtyi"=>$filter->rowqtyi,
                    "rowconnection1"=>$filter->rowconnection1,
                    "rowtop"=>$filter->rowtop,
                    "rowqtyt"=>$filter->rowqtyt,
                    "rowconnection2"=>$filter->rowconnection2,
                    "rowuop"=>$filter->rowuop,
                    "rowqtyu"=>$filter->rowqtyu,
                    "f_survey"=>$filter->f_survey,
                    "f_description"=>$filter->f_description,
                    "f_color_col"=>$filter->f_color_col,
                    "f_color_row"=>$filter->f_color_row
            );
            
            //echo $filter->id.$filter->coliop.$filter->colqtyi.$filter->colqtyi.$filter->colconnection1.$filter->coltop;
            //echo $filter->colqtyt.$filter->colconnection2.$filter->coluop.$filter->colqtyu;
            //echo "dungarray:".$filterdetail["coliop"];
            
            $colFilter = "";
            if(strcmp($filterdetail["coliop"],"0")){
                $colFilter .= "I ".$filterdetail["coliop"]." ".$filterdetail["colqtyi"];    
                if(strcmp($filterdetail["coltop"],"0") != "0"){
                    $colFilter .= $filterdetail["colconnection1"] ." T ".$filterdetail["coltop"]." ".$filterdetail["colqtyt"];
                    if (strcmp($filterdetail["coluop"],"0"))
                        $colFilter .= $filterdetail["colconnection2"] ." U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];
                }else if(strcmp($filterdetail["coluop"],"0")){
                    $colFilter .= $filterdetail["colconnection2"] ." U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];
                }
            }
            else if(strcmp($filterdetail["coltop"],"0")){
                $colFilter .= " T ".$filterdetail["coltop"]." ".$filterdetail["colqtyt"];
                if(strcmp($filterdetail["coluop"],"0")){
                    $colFilter .= $filterdetail["colconnection2"] ." U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];    
                }
            }else if(strcmp($filterdetail["coluop"],"0")){
                $colFilter .= " U ".$filterdetail["coluop"]." ".$filterdetail["colqtyu"];
            }
            
            $rowFilter = "";
            if(strcmp($filterdetail["rowiop"],"0")){
                $rowFilter .= "I ".$filterdetail["rowiop"]." ".$filterdetail["rowqtyi"];    
                if(strcmp($filterdetail["rowtop"],"0")){
                    $rowFilter .= $filterdetail["rowconnection1"] ." T ".$filterdetail["rowtop"]." ".$filterdetail["rowqtyt"];
                    if (strcmp($filterdetail["rowuop"],"0"))
                        $rowFilter .= $filterdetail["rowconnection2"] ." U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];
                }else if(strcmp($filterdetail["rowuop"],"0")){
                    $rowFilter .= $filterdetail["rowconnection2"] ." U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];
                }
            }
            else if(strcmp($filterdetail["rowtop"], "0")){
                $rowFilter .= " T ".$filterdetail["rowtop"]." ".$filterdetail["rowqtyt"];
                if(strcmp($filterdetail["rowuop"], "0")){
                    $rowFilter .= $filterdetail["rowconnection2"] ." U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];    
                }
            }else if(strcmp($filterdetail["rowuop"], "0")){
                $rowFilter .= " U ".$filterdetail["rowuop"]." ".$filterdetail["rowqtyu"];
            }
            

            $paramForToggleFilterMethod = 
                    $filterdetail["coliop"]."/".
                    $filterdetail["colqtyi"]."/".
                    $filterdetail["colconnection1"]."/".
                    $filterdetail["coltop"]."/".
                    $filterdetail["colqtyt"]."/".
                    $filterdetail["colconnection2"]."/".
                    $filterdetail["coluop"]."/".
                    $filterdetail["colqtyu"]."/".
                    $filterdetail["rowiop"]."/".
                    $filterdetail["rowqtyi"]."/".
                    $filterdetail["rowconnection1"]."/".
                    $filterdetail["rowtop"]."/".
                    $filterdetail["rowqtyt"]."/".
                    $filterdetail["rowconnection2"]."/".
                    $filterdetail["rowuop"]."/".
                    $filterdetail["rowqtyu"]."/".
                    $filterdetail["f_color_col"]."/".
                    $filterdetail["f_color_row"];
                    
            echo $filterdetail["f_description"];
            echo "<input type=\"checkbox\" name=\"cbx_filter_enabler\" id=\"cbx_filter_enabler\" onchange=\"parent.refreshFilters();\" value=\"".$paramForToggleFilterMethod."\"/>";
            echo "<a href=\"#\" onclick=\"parent.deleteFilter(".$filterdetail["id"].");return false;\">X</a>";
            echo "<div style=\"width:200px;background-color:".$filterdetail["f_color_col"].";\">".$colFilter."</div>";
            echo "<div style=\"width:200px;background-color:".$filterdetail["f_color_row"].";\">".$rowFilter."</div>";            
            echo "-----------------END-----------------<br/>";
        }
}

function colorizeLevelLo($level){
    switch ($level) {
    case 1:
        return COLOR_LEV1;
    case 2:
        return COLOR_LEV2;
    case 3: 
        return COLOR_LEV3;
    case 4:
        return COLOR_LEV4;
    case 5:
        return COLOR_LEV5;
    case 6:
        return COLOR_LEV6;
    case 7:
        return COLOR_LEV7;
    default:
       return "#FFFFFF"; 
    }
}

function displayChildLoDownToLev($survey_id, $lev, $deepest, $parentLo, $dblib, &$LoStrArray, $colSpanOfUpperLev, &$DeepestLOsCodeArray, &$DeepestLOsIdArray){
        $childLos = $dblib->getLoOfLevelOfSurvey($survey_id, $lev, $parentLo);
        
        
        $accumulator = $colSpanOfUpperLev;
        foreach($childLos as $childLo){
                    if(($lev) == $deepest){
                        //echo "foreach lev ".$lev."--".$childLo->sl_code."<br/>";
                        $accumulator += 1;
                        $LoStrArray[$lev] .=
                            "<td border=\"1\" align=\"center\" name=\"-lo".$childLo->sl_lo."\" id=\"-lo".$childLo->sl_lo."\">".$childLo->sl_code."&nbsp;</td>";
                        array_push($DeepestLOsCodeArray, $childLo->sl_lo);
                        array_push($DeepestLOsIdArray, $childLo->id);
                    }else if(($lev) < $deepest && ($dblib->hasLoChild($childLo->sl_code) <= 0)){
                        //Never happen excep data has no integrity
                        
                    }else if(($lev) < $deepest && ($dblib->hasLoChild($childLo->sl_code) > 0)){
                        $childLos = $dblib->getLoOfLevelOfSurvey($survey_id, $lev+1, $childLo->sl_code);
                        if(($lev+2) <= $deepest){
                            $numberOfChildren = displayChildLoDownToLev($survey_id, $lev+1, $deepest, $childLo->sl_code, $dblib, $LoStrArray, $colSpanOfUpperLev, $DeepestLOsCodeArray, $DeepestLOsIdArray);
                            $accumulator += $numberOfChildren;
                            
                            $LoStrArray[$lev] .= "<td border=\"1\" align=\"center\" colspan=\"".$numberOfChildren."\" bgcolor=\"".colorizeLevelLo($lev)."\">".$childLo->sl_code."&nbsp;</td>";
                        }else if(($lev+1) == $deepest){
                            //echo "<br/>children of ".$childLo->sl_code.":".count($childLos)."-lev:".$lev."<br/>";
                            $numberOfChildren = displayChildLoDownToLev($survey_id, $lev+1, $deepest, $childLo->sl_code, $dblib, $LoStrArray, $colSpanOfUpperLev, $DeepestLOsCodeArray, $DeepestLOsIdArray);
                            $accumulator += $numberOfChildren;
                            $LoStrArray[$lev] .= "<td border=\"1\" align=\"center\" colspan=\"".count($childLos)."\" bgcolor=\"".colorizeLevelLo($lev)."\">".$childLo->sl_code."&nbsp;</td>";
                        }else{
                            echo "<br/>lev:".$lev."---".$deepest."<br/>";
                        }
                    }                
        }           
        return $accumulator;
}
?>